package com.hongyuan.sellcoach.ui.course.view_mode;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellcoach.R;
import com.hongyuan.sellcoach.base.CustomViewModel;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.data.MyRepository;
import com.hongyuan.sellcoach.myview.scllor_view.UnitBean;
import com.hongyuan.sellcoach.myview.scllor_view.UnitBeanUtils;
import com.hongyuan.sellcoach.ui.course.beans.OsBeans;
import com.hongyuan.sellcoach.ui.course.beans.SalesPerformanceBean;
import java.util.List;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class SalesPerformanceViewMode extends CustomViewModel<MyRepository> {

    //课程选取工具
    public UnitBeanUtils osUtils;
    //课程数据集
    private ObservableField<List<OsBeans.ListBean>> osList = new ObservableField<>();

    public ObservableField<SalesPerformanceBean> entity = new ObservableField<>();

    public SalesPerformanceViewMode(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //显示时间段选择器
        public SingleLiveEvent<Void> showPeriod = new SingleLiveEvent<>();
        //显示门店列表
        public SingleLiveEvent<List<UnitBean>> showOs = new SingleLiveEvent<>();
    }

    //给RecyclerView添加ObservableList
    public ObservableList<SalesPerformanceItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<SalesPerformanceItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_sales_performance);

    //选着时间段
    public BindingCommand selectPeriod = new BindingCommand(() -> {
        uc.showPeriod.call();
    });

    //选着门店
    public BindingCommand selectOs = new BindingCommand(() -> {
        if(osUtils != null && osList.get() != null && osList.get().size() > 0){
            uc.showOs.setValue(osUtils.getUnitList(osList.get()));
        }else{
            ToastUtils.showShort("暂无选门店");
        }

    });

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.ORDER_ACHIEVEMENT){
            SalesPerformanceBean performanceBean = (SalesPerformanceBean)dataBean;
            entity.set(performanceBean);

            List<SalesPerformanceBean.ListBean> list = performanceBean.getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();

            }

            if(list != null && list.size() > 0){
                for(SalesPerformanceBean.ListBean bean : list){
                    SalesPerformanceItemViewModel itemViewModel = new SalesPerformanceItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(list == null || list.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }
        }

        if(code == Constants.OS_LIST){
            OsBeans osBeans = (OsBeans)dataBean;
            OsBeans.ListBean  listBean = new OsBeans.ListBean();
            listBean.setOs_name("全部门店");
            osBeans.getList().add(0,listBean);
            osList.set(osBeans.getList());
            osList.set(osBeans.getList());

            osUtils = new UnitBeanUtils<OsBeans.ListBean>() {
                @Override
                public String unit(OsBeans.ListBean o) {
                    return String.valueOf(o.getOs_id());
                }

                @Override
                public String unit_cn(OsBeans.ListBean o) {
                    return o.getOs_name();
                }
            };
        }


    }
}
